var uploadArt;
$(function () {
    uploadArt = new Vue({
        el:'#uploadArtController',
        data:{
            name:'',
            keyword:'',
            keywords:[],
            year:'',
            price:'',
            size:{width:'',height:'',high:''},
            info:[
                {
                    content:'',
                    img:''
                }
            ],
            keys:[],
            infoIndex:[]
        },
        methods:{
            loadKeywords:function () {
                var that = this;
                sy.postApi('/api/artLibrary/getGoodsTypeList',{},function (data) {
                    that.keywords = data;
                })
            },
            selectKey:function (item) {
                if (this.hasKey(item.id)){
                    this.keys.remove(item.id);
                }else{
                    if(this.keys.length >= 10){
                        sy.msg("分类最多选择10个");
                        return;
                    }
                    this.keys.push(item.id);
                }
            },
            hasKey:function (key) {
                for (var i  = 0; i < this.keys.length; i++){
                    if (key == this.keys[i]){
                        return true;
                    }
                }
                return false;
            },
            uploadImg:function (item,index) {
                $("#img").trigger("click");
                this.infoIndex = index;
            },
            delImg:function (itemIndex,imgIndex) {
                var item = this.info[itemIndex];
                if (item){
                    var imgs = item.img.split(',');
                    imgs.splice(imgIndex,1);
                    item.img = imgs.join(',');
                }
            },
            delItem:function (itemIndex) {
                var item = this.info[itemIndex];
                if (item){
                    this.info.splice(itemIndex,1);
                }
            },
            updateImgs:function (id) {
                var item = this.info[this.infoIndex];
                if (item){
                    var arr  = item.img?item.img.split(','):[];
                    arr.push(id);
                    item.img = arr.join(',');
                    console.log(item.img);
                    $("#img").val("");
                    sy.closeLoad();
                }
            },
            addInfo:function () {
                var json = {
                    content:'',
                    img:''
                };
                this.info.push(json);
            },
            addArt:function () {
                var that = this;
                if (!that.name){
                    sy.msg('请输入艺术品名称');
                    return;
                }
                if (that.keys.length == 0){
                    sy.msg('请选择分类');
                    return;
                }
                // if (!that.year){
                //     sy.msg('请输入作品年份');
                //     return;
                // }
                // if (!that.price){
                //     sy.msg('请输入作品底价');
                //     return;
                // }
                if (!that.size.height){
                    sy.msg('请输入艺术品长度');
                    return;
                }
                if (!that.size.width){
                    sy.msg('请输入艺术品宽度');
                    return;
                }
                // if (!that.size.high){
                //     sy.msg('请输入作品高度');
                //     return;
                // }
                var hasInfo = false;
                var imgs = [];
                $.each(that.info,function(i,o){
                    if (o.img.length > 0){
                        imgs = imgs.concat(o.img.split(','));
                    }
                    if (o.content){
                        hasInfo = true;
                    }
                });

                if (!hasInfo){
                    sy.msg('请输入艺术品简介');
                    return;
                }

                if (imgs.length == 0){
                    sy.msg('请上传艺术品图片');
                    return;
                }

                sy.postApi("/api/index/uploadGoods",{
                    goodsName:that.name,
                    goodsType:that.keys.join(','),
                    goodsTime:that.year,
                    lowPrice:that.price,
                    goodsInfo:sy.jsonToString(that.info),
                    height:that.size.height,
                    width:that.size.width,
                    high:that.size.high,
                    imgs:imgs.join(',')
                },function (data) {
                    sy.msg("您的艺术品已提交审核",function () {
                        location.href = sy.path+'/web/home';
                    })

                })
            }
        },
        created:function () {
            this.loadKeywords();
        },
        mounted:function () {
            var that = this;
            $("#img").bind("change",function(){
                if($(this).val() == '')return;
                sy.openLoad();
                $("#form1").submit();
            });
            laydate.render({
                elem: '#year', //指定元素
                type: 'year',
                max:0,
                done: function(value){
                    that.year = value;
                }
            });
        }
    })
});

/**
 * 图片上传回调
 * @param id
 */
function callback(id) {
    uploadArt.updateImgs(id);
}